package com.ccb.sc.common.modules.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ccb.sc.common.bind.page.PageParams;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.modules.goods.convert.GoodsCommentConvert;
import com.ccb.sc.common.modules.goods.entity.GoodsComment;
import com.ccb.sc.common.modules.goods.enums.GoodsType;
import com.ccb.sc.common.modules.goods.mapper.GoodsCommentMapper;
import com.ccb.sc.common.modules.goods.service.IGoodsCommentService;
import com.ccb.sc.common.modules.goods.vo.GoodsCommentStatVo;
import com.ccb.sc.common.modules.goods.vo.GoodsCommentVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ken
 * @version 1.0
 * @date 2023/10/15 15:16
 */
@Service
public class GoodsCommentServiceImpl  extends ServiceImpl<GoodsCommentMapper, GoodsComment> implements IGoodsCommentService {

    @Override
    public GoodsCommentStatVo getGoodsDetailCommentStat(Long goodsId, GoodsType goodsType) {
        GoodsCommentStatVo statVo = new GoodsCommentStatVo();
        long count = this.count(new QueryWrapper<GoodsComment>()
                .lambda()
                .eq(GoodsComment::getGoodsType, goodsType)
                .eq(GoodsComment::getGoodsId, goodsId)
                .eq(GoodsComment::getIsShow, true));
        statVo.setTotals((int) count);
        if (count == 0) {
            return statVo;
        }
        GoodsComment goodsComment = this.getOne(new QueryWrapper<GoodsComment>()
                .lambda()
                .eq(GoodsComment::getGoodsId, goodsId)
                .eq(GoodsComment::getIsShow, true)
                .orderByDesc(GoodsComment::getCreateTime)
                .last("limit 1"));
        if (goodsComment != null) {
            if (goodsComment.getIsAnonymous()) {
                int index = 1;
                if (goodsComment.getNickname().length() > 4) {
                    index = 2;
                }
                String hideNickname = StrUtil.hide(goodsComment.getNickname(), index, goodsComment.getNickname().length() - index);
                statVo.setNickname(hideNickname);
            }else {
                statVo.setNickname(goodsComment.getNickname());
            }
            statVo.setAvatar(goodsComment.getAvatar());
            statVo.setContent(goodsComment.getContent());
            statVo.setPictures(goodsComment.getPictures());
        }
        return statVo;
    }

    @Override
    public PageVo<GoodsCommentVo> comments(Long goodsId, GoodsType goodsType, PageParams pageParams) {
        Page<GoodsComment> page = new Page<>(pageParams.getCurrent(), pageParams.getPageSize());
        page.setOrders(Lists.newArrayList(OrderItem.desc("create_time")));
        QueryWrapper<GoodsComment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(GoodsComment::getIsShow, true)
                .eq(GoodsComment::getGoodsId, goodsId)
                .eq(GoodsComment::getGoodsType, goodsType);
        Page<GoodsComment> commentPage = this.page(page, wrapper);
        if (CollectionUtils.isEmpty(commentPage.getRecords())) {
            return PageVo.create(pageParams.getCurrent(), pageParams.getPageSize(), 0, Lists.newArrayList());
        }

        List<GoodsCommentVo> list = commentPage.getRecords().stream().map(item -> {
            GoodsCommentVo vo = GoodsCommentConvert.INSTANCE.entity2Item(item);
            if (item.getIsAnonymous()) {
                int index = 1;
                if (item.getNickname().length() > 4) {
                    index = 2;
                }
                String hideNickname = StrUtil.hide(item.getNickname(), index, item.getNickname().length() - index);
                vo.setNickname(hideNickname);
            }
            return vo;
        }).collect(Collectors.toList());
        return PageVo.create(pageParams.getCurrent(), pageParams.getPageSize(), commentPage.getTotal(), list);
    }
}
